<?php
$form_name="articles_form"."_";
if( Form::isRepeatedSubmit()) Page::message( 'warning', null, Lang::get('PREVENT_REPEATED_SUBMIT') );
	else
if( isset($_REQUEST[ $form_name."posted"]) ){
	$_SESSION['posttime']=$_REQUEST['posttime'];
	
	$table = DB::$tables['articles'];
	/**/
	if( empty($_REQUEST['articles_id'])){
		$_REQUEST[ "articles_id"] = 0;
		$_REQUEST[ "articles_crt_date"] = date("Y-m-d H:i:s");
	}
	
	$checkMandatory = DB::checkMandatory( $table, $_REQUEST ); //myPrint($checkMandatory,'$checkMandatory'); myPrint($_REQUEST,'$_REQUEST');
	if(count($checkMandatory)>0){
		Page::message( 'error', null, Lang::get('REQUIRED_FIELDS') );
		foreach($checkMandatory as $name=>$value){
			Page::message( 'error', $name, $value );
			Form::error( $form_name, $name, Lang::get('REQUIRED_FIELDS') );
		}	
	}	
	
	if(count(Page::$messages['error'])<1){
		try{
			$_REQUEST[ "articles_chg_date"] = date("Y-m-d H:i:s");
			if( empty($_REQUEST['articles_id'])){
				unset($_REQUEST[ "articles_id"]);
				$_REQUEST[ "articles_crt_date"] = date("Y-m-d H:i:s");
			}
			
			$requestArr = DB::createRequestArr( array($table) ); //myPrint( $requestArr[ $table ], '$requestArr[ $table ]');
			
			if( !empty($_REQUEST['articles_id'])){
				$operation = Lang::get('TO_EDIT').' "'.$_REQUEST['articles_title'].'"';
				DB::updateTableArrayWhere( $table, $requestArr[ $table ], 'articles_id="'.$_REQUEST['articles_id'].'"' );		
			}else{
				$operation = Lang::get('TO_ADD').' "'.$_REQUEST['articles_title'].'"';
				DB::insertTableArray( $table, $requestArr[ $table ] );
			}
			
			Page::message( 'success', null, Lang::get('OPERATION_IS_EXECUTED').': '.$operation );
		}catch(Exception $e){
			Page::message( 'error', null, Lang::get('ERROR_OF_EXECUTION').': '.$operation );
		}	
	}
	else
	foreach( $_REQUEST as $name=>$value) Form::value( $form_name, $name, $value);
}

global $pageTitle;	
if(!empty( $_REQUEST['articles_id'] )){
	$sql = 'SELECT * FROM '.DB::$tables['articles'].
		' WHERE `articles_id`='.intval( $_REQUEST['articles_id'] );
	$_article = exec_sql2array($sql); //myPrint( $article, '$article');
	
	if(count($_article)>0){
		$article  = $_article[0];
		foreach( $article as $name=>$value ) Form::value( $form_name, $name, $value);
	}
	
	$pageTitle = Lang::get('TO_EDIT');
}
else $pageTitle = Lang::get('TO_CREATE');

echo '<h3>'.$pageTitle.':</h3>';
	
?>

<script language="javascript" type="text/javascript" src="/admin/editarea/edit_area/edit_area_full.js"></script>
<script language="javascript" type="text/javascript" src="/admin/editarea.php?editarea_id=articles_text"></script>

<?php echo Page::outMessages(); echo Form::outErrors( articles_form );?>
<form  method="post" class="panel" name="<?php echo $form_name?>" action="">
<input type="hidden" name="posttime" value="<?php echo time();?>" />
<input type="hidden" name="<?php echo $form_name?>posted" value="1"/>
<input type="hidden" name="articles_id" id="articles_id" value="<?php echo Form::value( $form_name, "articles_id");?>" >
<input type="hidden" name="articles_author_id" id="articles_author_id" value="<?php echo Form::value( $form_name, "articles_author_id") || $_SESSION['user']['user_id'];?>" >
<input type="hidden" name="articles_editor_id" id="articles_editor_id" value="<?php echo $_SESSION['user']['user_id'];?>" >
<input type="hidden" name="articles_crt_date" id="articles_crt_date" value="<?php echo Form::value( $form_name, "articles_crt_date");?>" >
<input type="hidden" name="articles_chg_date" id="articles_chg_date" value="<?php echo Form::value( $form_name, "articles_chg_date");?>" >
	<table width="100%" class="form_table">
	<tbody>
<tr>
	<td width="50">Категория<b style="color:red">*</b></td>
	<td>
		<select name="articles_category" id="articles_category" value="<?php echo Form::value( $form_name, "articles_category");?>" >
		<?php
			$opAr = array(''=>'')+Form::getEnumArray( 'articles', 'articles_category');
			echo Form::createSelectOptions( 
				$opAr,
				( isset( $article, $article['articles_category'] ) ? $article['articles_category'] : null)
			);	
		?>		
		</select>
	</td>
</tr>
<tr>
	<td width="50">Публикуется<b style="color:red">*</b></td>
	<td>
		<select name="articles_publicated" id="articles_publicated" value="<?php echo Form::value( $form_name, "articles_publicated");?>" >
		<?php
		echo Form::createSelectOptions( 
				array('0'=>Lang::get('NO'), '1'=>Lang::get('YES'),), 
				( isset( $article, $article['articles_publicated'] ) ? $article['articles_publicated'] : null)
			);
		?>
		</select>
	</td>
</tr>
<tr>
	<td width="50">Заголовок<b style="color:red">*</b></td>
	<td>
		<input type="text" name="articles_title" id="articles_title" value="<?php echo Form::value( $form_name, "articles_title");?>" >
	</td>
</tr>
<tr>
	<td width="50">Текст, HTML</td>
	<td>
		<textarea style="width:99%" name="articles_text" id="articles_text" ><?php echo Form::value( $form_name, "articles_text");?></textarea>
	</td>
</tr>
	</tbody>	
	</table>
	<input type="submit" name="submit" value="<?php echo Lang::get('TO_SAVE')?>"/>
</form>